package com.xp.flower.qsd.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xp.flower.api.IFlowerPraiseService;
import com.xp.flower.common.ajax.AjaxError;
import com.xp.flower.common.ajax.AjaxResponse;
import com.xp.flower.common.dto.FlowerPraiseDTO;
import com.xp.flower.common.dto.UserDTO;
import com.xp.flower.qsd.consts.WebConsts;

@RequestMapping("flowerPraiseController")
@Controller
public class FlowerPraiseController {
    
    @Autowired
    private IFlowerPraiseService flowerPraiseService;
    
    @RequestMapping(value = "addOrChannelPraise", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse<FlowerPraiseDTO> addOrChannelPraise(
           @RequestParam(value = "flowerId", required = true)Long flowerId,
           @RequestParam(value = "praiseOrChannel",required = true)Integer praiseOrChannel,
           @RequestParam(value = "id",required = false)Long id,
           HttpSession session)
    {
        
        AjaxResponse<FlowerPraiseDTO> reponse = null;
        UserDTO userDTO = ( UserDTO)session.getAttribute(WebConsts.USERINFO);
        
        if(null == userDTO){
            //提示用户跳转到登录页面
        }else{
            //喜欢
            if(praiseOrChannel == 1){
                FlowerPraiseDTO flowerPraiseDTO = new FlowerPraiseDTO();
                flowerPraiseDTO.setUserId(userDTO.getId());
                flowerPraiseDTO.setCreatetime(System.currentTimeMillis());
                flowerPraiseDTO.setFlowerId(flowerId);
                Long primaryId = flowerPraiseService.addPraise(flowerPraiseDTO);
                flowerPraiseDTO.setId(primaryId);
                if(primaryId > 0){
                    reponse = new AjaxResponse<FlowerPraiseDTO>(AjaxError.PRAISESUCCESS.getCode(), AjaxError.PRAISESUCCESS.getMessage());
                    reponse.setT(flowerPraiseDTO);
                }else{
                    reponse = new AjaxResponse<FlowerPraiseDTO>(AjaxError.PRAISESUCCESS.getCode(), AjaxError.PRAISESUCCESS.getMessage());
                }
            }else{
                flowerPraiseService.channelPraise(id);
            }
            
        }
        return reponse;
    }

}
